﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Awesomium.Core;

namespace SafeBrowser.ComponentModel
{
    class SafeInterceptor : IResourceInterceptor
    {
        private readonly IEnumerable<string> _keywords;
        private readonly string _errorPagePath;

        public SafeInterceptor()
        {
            var keywords = Properties.Resources.keywords;
            _keywords = keywords.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            _errorPagePath = string.Format("{0}\\Resources\\errorPage.htm", Environment.CurrentDirectory);
        }


        public ResourceResponse OnRequest(ResourceRequest request)
        {
            if (request.Any(item =>item.IsFilePath || _keywords.Any(keyword => item.Bytes.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) > -1)))
            {
                return ResourceResponse.Create(_errorPagePath);
            }

            return null;
        }

        public bool OnFilterNavigation(NavigationRequest request)
        {
            return false;
        }
    }
}
